home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / ARexxGuide / ARx_Cmpr.rexx < prev    next >
OS/2 REXX Batch file  |  1994-03-12  |  3KB  |  102 lines

  1. /*    $VER: 1.0 ARx_Cmpr.rexx by Robin Evans (31 May 1993) */
  2.  
  3. /*    Demonstrate the effect of different comparison operations */
  4.  
  5. arg close
  6.  
  7. csi='9b'x;slant=csi'3m';bold=csi'1m';norm=csi'0m';black=csi'31m';white=csi'32m';blue=csi'33m'
  8. LF = '0a'x
  9. options prompt white':::' black
  10.  
  11. say white'This demonstration allows you to enter a pair a values'
  12. say 'to see how they fare in various comparison tests.'
  13. say 'The file that runs the demo is called "ARx_Cmpr.rexx"'
  14. say 'and is located in your rexx: directory.'
  15. say 'Type'black 'Q' white || 'at any "::: " prompt to exit the application' '0a0a'x || norm
  16.  
  17. do forever
  18.     say LF||white'Enter two values to be compared.'
  19.     say blue'   Enclose a value that includes a space in double { " } quotes'
  20.     parse pull v
  21.     parse var v '"' val.1 '"' '"' val.2 '"' , '"' . '"' val.3 ., val.4 val.5 .
  22.     trace off
  23.  
  24.     vari. = ''
  25.     vn = 0
  26.     do i = 1 to 5 until vn = 1
  27.         if val.i > '' then do
  28.             vn = vari.0 > ''
  29.             vari.vn = val.i
  30.         end
  31.     end
  32.     if vn < 1 then do
  33.         if upper(vari.0) = 'Q' then leave
  34.         say lf||black'You must enter two values to be compared.'
  35.         say '   Enter <Q> to leave the demonstration.'
  36.         iterate
  37.     end
  38.         /*
  39.             the expression below uses a series of comparison operations to arrive
  40.             at a number which indicates whether the string are equal, >, or <.
  41.             The same operation could have been performed with a select instruction.
  42.             That would even be easier to decipher. This method is offered just
  43.             'because it was there'.
  44.         */
  45.     numeric digits 14
  46.     Cmprsn = (vari.0 = vari.1) + (vari.0 == vari.1) * 2 + (vari.0 > vari.1) * 4,
  47.              + (vari.0 < vari.1) * 8
  48.     numeric digits
  49.  
  50.     Cmpr.1 = 'is equal to'
  51.     Cmpr.3 = 'is exactly equal to'
  52.     Cmpr.4 = 'is greater than'
  53.     Cmpr.8 = 'is less than'
  54.  
  55.     say ''
  56.     Say '"'vari.0'"' Cmpr.Cmprsn '"'vari.1'"'
  57.     say ''
  58.  
  59.     select
  60.         when Cmprsn = 1 then do
  61.             say "Leading and trailing spaces are ignored in normal"
  62.             say "comparison operations, but not when the strict comparison"
  63.             say "operators are used."
  64.             say white"   This is true:"
  65.             say black"      "vari.0 '=' vari.1
  66.             say white"   This is false:"
  67.             say black"      "vari.0 '==' vari.1
  68.         end
  69.         when datatype(vari.0, N) && datatype(vari.1, N) then do
  70.             say "Since only one of the values is numeric, the comparison"
  71.             say "is made on the ASCII character values of each string."
  72.             say "Numerals have a lower ASCII value than alphabetic characters."
  73.         end
  74.         when datatype(vari.0, N) then
  75.             say "A numeric comparison was performed."
  76.         otherwise
  77.             say "The comparison was character-based."
  78.             if Cmprsn > 3 then do
  79.                 DifPos = compare(vari.0, vari.1)
  80.                 do i = 0 to 1
  81.                     DifChar.i = substr(vari.i, DifPos, 1)
  82.                 end
  83.                 BigChar = (DifChar.1 > DifChar.0); LtlChar = ~BigChar
  84.                 say "The two strings are different at character" DifPos":"
  85.                 say 'Character "'DifChar.BigChar'" in "'vari.BigChar'" has an ASCII value'
  86.                 say 'of' c2d(DifChar.BigChar) 'compared to an ASCII value of' c2d(DifChar.LtlChar) 'for'
  87.                 say 'the character "'DifChar.LtlChar'" in the other string.'
  88.  
  89.                 if length(vari.0) ~= length(vari.1) then
  90.                 do
  91.                     lngr = (length(vari.1) > length(vari.0)); shrtr = ~lngr
  92.                     say LF||white'      Note that since "'vari.lngr'" is a longer string,'
  93.                     say '      "'vari.shrtr'" was expanded to' length(vari.lngr) 'characters by padding'
  94.                     say '      it with spaces before the comparison.'norm
  95.                 end
  96.             end
  97.     end
  98. end
  99. if close = 'CLOSE' then
  100.     address command 'endcli'
  101. return 0
  102.